﻿var messageManager = {
    _id: 0,
    _caches: {},

    create: function (once, options) {
        var self = this;
        var _newId = "message_" + (++this._id).toString();
        var _ctx = null;
        var _dispose = !!once;
        var _obj = this._caches[_newId] = {
            initialize: function (opt) {
                _ctx = $("<div></div>", { id: _newId }).appendTo(document.body);
                _ctx.dialog({
                    title: "",
                    dialogClass: (typeof (opt.class) === "undefined" ? "" : opt.class),
                    width: opt.width || 300,
                    height:opt.height||"auto",
                    minWidth: opt.minWidth || 150,
                    maxWidth: opt.maxWidth || 600,
                    bgiframe: true,
                    autoOpen: false,
                    closeOnEscape: opt.closeOnEscape || true,
                    modal: (typeof (opt.modal) === "undefined" ? true : !!opt.modal),
                    stack: true,
                    resizable: (typeof (opt.resizable) === "undefined" ? false : !!opt.resizable),
                    buttons: [],
                    close: function () {
                        if (_dispose) { setTimeout(function () { _obj.destroy(); }, 0); }
                    }
                });
            },
            get: function () { return _ctx; },
            show: function () { _ctx.dialog("open"); },
            close: function () { _ctx.dialog("close"); },
            destroy: function () {
                delete self._caches[_newId];
                _ctx.dialog("destroy");
                _ctx.remove();
                _ctx = null;
                _obj = null;
                self = null;
            },

            custom: function (title, msg, btns) {
                //btns = [{ text: "", click: function (fn) { } }];
                _ctx.html(msg);
                for (var i = 0, j = btns.length, btn; i < j; i++) {
                    btn = btns[i];
                    btn.click = (function (callback) {
                        return function () {
                            var isFn = (typeof (callback) === "function");
                            var resultFn = isFn ? callback.call(this, _obj.close) : false;
                            if (!resultFn) {
                                _obj.close();
                            }
                        };
                    })(btn.click);
                }

                _ctx.dialog("option", {
                    title: title,
                    buttons: btns
                });
                this.show();
            },

            info: function (msg) { this.custom("提示", "<div style=\"margin:4px;\">"+msg+"</div>", [{ text: "确定" }]); },
            //info: function (msg) { this.custom("提示", msg, [{ text: "确定" }]); },
            warn: function (msg) { this.custom("警告", msg, [{ text: "确定" }]); },
            error: function (msg) { this.custom("错误", msg, [{ text: "确定" }]); },
            confirm: function (msg, callback) {
                //callback返回除true之外的任意值，则需要立即关闭对话框。否则由callback控制(callback接收关闭对话框的方法)何时关闭对话框。
                this.custom("确认", msg, [{ text: "确定", click: callback }, { text: "取消" }]);
            }
        };
        _obj.initialize(options || {});
        return _obj;
    },
    destroy: function () {
        var messageContext;
        for (var k in this._caches) {
            messageContext = this._caches[k];
            messageContext.destroy();
            this._caches[k] = null;
        }
        this._caches = {};
        this._instance = null;
        this._id = 0;
    },

    instance: function () {
        if (!this._instance) { this._instance = this.create(false); }
        return this._instance;
    },
    close: function () {
        this.instance().close();
    },

    info: function (msg) { this.instance().info(msg); },
    warn: function (msg) { this.instance().warn(msg); },
    error: function (msg) { this.instance().error(msg); },
    confirm: function (msg, callback) { this.instance().confirm(msg, callback); },

    alert: function (msg) { this.info(msg); },
    delay: function (msg, timeout) {
        var w = this.create(true, { modal: false, class: "message_delay" });
        w.custom("提示", msg, []);
        setTimeout(function () { w.close(); }, timeout);
    }
};